<?php
session_start();
include_once 'classes/DBCon.php';
include_once 'classes/Forum.php';
include_once 'classes/Announcement.php';
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Railway Official Website</title>
        <link rel="stylesheet" href="css/foundation.css"/>
        <link rel="stylesheet" href="css/normalize.css"/>
        <link rel="stylesheet" href="css/jquery-ui.css" />
        <script src="js/vendor/jquery.js"></script>
        <script src="js/ui/jquery-ui.js"></script>
        <script src="js/vendor/custom.modernizr.js"></script>
        <script src="js/forum.js"></script>
        <style type="text/css">
		#wrap {display:table;height:100%}
        fieldset { border:2px solid grey }
        legend {
              padding: 0.2em 0.5em;
              border:2px solid white;
              color:black;
              font-size:150%;
              text-align:left;
        }
	</style>
        


    </head>
    <body bgcolor="transparent">
        
        <div>
            <nav class="top-bar">
                <ul class="title-area">
                    <li class="name" onClick="void(0);">
                        <h1><a href="ForumMain.php">Railway Community Forum</a></h1>
                    </li>
                    <li class="divider"></li>
                    <li class="toggle-topbar menu-icon">
                        <a href="#"><span>menu</span></a>
                    </li>
                </ul>
                <section class="top-bar-section">
                    <ul class="right">
                        <li class="divider"></li>
                        <li><a href="index.php">Home</a></li>
                        <li class="divider"></li>
                        <?php
                        if (isset($_SESSION['username'])) {
                            echo '<li><a href="profile.php">Welcome, ' . $_SESSION['username'] . '!</a></li>';
                            echo '<li class="divider"></li>';
                            echo '<li><a href="logout.php">Logout</a></li>';
                            echo '<li class="divider"></li>';
                        }
                        else{
                            echo '<li><a href="loginPage.php">Login</a></li>';
                            echo '<li class="divider"></li>';
                            echo '<li><a href="register.php">Register</a></li>';
                            echo '<li class=divider"></li>';
                        }
                        ?>
                    </ul>
                </section>
            </nav>
        </div>
      
        <div id="refreshPost2">
            <?php
            if(isset($_SESSION['username'])){
                $thread = new Forum();
                $thread->refreshPostCount();
                $thread->viewThreads($_REQUEST['permalink']);
                
                
                
//                
//
//                $s = new Schedule();
//            $schedule = $s->obtainSchedules();
//            
//           sort($schedule);
//           print_r($schedule);
//
//                
                
//                include 'classes/User.php';
//                $a = new User();
//                $ba = $a->sendEmail2("littlenosaj@gmail.com", "<p>bello</p>");
//                echo $ba;
//                $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
//                
//                echo $url;

//                include_once 'classes/Member.php';
//                $m = new Member();
//                $arr = $m->getAllMemberEmail();
//                $a = new Announcement();
//                
//                while($arr){
//                    $a->sendEmail(array_pop($arr), 'hello', 'content');
//                }
//                $a = new Announcement();
//                if($a->updateAnnouncement('tttt', 'mmmmm', 'ttttt', 'gggraphic', 'vvvvv', 6)){
//                    echo '<p>TRUE</p>';
//                }else{
//                    echo '<p>FALSE</p>';
//                }
//                $a->populateAnnouncementById(3);
//                echo $a->getVideoEmbLink();
                
//                $a = new Announcement();
//                if($a->createAnnouncment('$title', '$message', 5, '$type', '$graphic', '$videoEmbLink')){
//                    echo '<p>true</p>';
//                }else{
//                    echo '<p>false</p>';
//                }
                
//                $title =  'title';
//                $message = 'message';
//                $type = 'type';
//                $image = 'imgFile';
//                $videoLink = 'videoLink';
//                $uid = $_SESSION['uid'];
//                $ann = new Announcement();
//
//                if($ann->createAnnouncment($title, $message, $uid, $type, $image, $videoLink)){
//                    echo '<p>true a</p>';
//                }else{
//                    echo '<p>false a</p>';
//                }
                
                
            }else{
                 header('Location: 404.php');
                 
            }     
            //$thread->createThread(1, 5, 'HAAelp me plz', 'help lo i say help!');
            //echo $thread->getNewThreadID();
            //echo $thread->getNewPostID();
            /*$a = $thread->insertPost(3, 2, '1wawww');
            echo $thread->getNewPostID2();
            if($a==true){
                echo '<p>true</p>';
            }else{
                echo '<p>false</p>';
            }*/
            //$a = $thread->insertPost(3, 2, 'wewww');
            //echo $thread->getUserJoinedDate(2);
            //echo $thread->getForumLatestPost(2);
            //echo 'a'+$thread->getTotalThread(2);
            ?>
        </div>

        <script type="text/javascript" >
            
            function displayModifyComment(pid){
                $("#tableEditComment" + pid).show();
            }
            
            function hideModifyComment(pid){
                $("#tableEditComment" + pid).hide();
            }
            
            function modifyPost(pid) {
                var comments2 = document.getElementById('commentModify'+pid).value;
                var newMsg = document.getElementById('commentModify'+pid).value;
                alert(newMsg);
                if(comments2===""){
                    $('#commentModify'+pid).addClass('error');
                    $('#commentModify'+pid+'Err').text("Required Field").show();
                }else{
                    $.ajax({
                    type: 'POST',
                    url: 'action.php',
                    data: {action: 'modifyPost', pid: pid, message:newMsg},
                    success: function(result) {
                        if (!result.error) {
                            document.getElementById('comment').value="";
                            $('#commentModify'+pid+'Err').fadeIn('fast').addClass('success').removeClass('alert').html(result.message).delay(4000).fadeOut('slow');
                            location.reload(window.location);
                            delay(5000);                                             
                        } else {
                            $('#commentModify'+pid+'Err').fadeIn('slow').addClass('alert').removeClass('success').html(result.message).delay(4000).fadeOut('slow');
                        }
                            
                    }
                    });
                }
                }
            
            
            function banUser(uid){
                if(confirm('Are you sure wan to ban this member?!')){
                    $.ajax({
                    type: 'POST',
                    url: 'action.php',
                    data: {action: 'banUser', uid: uid},
                    success: function(result) {
                        if (!result.error) {
                            location.reload(window.location);
                            alert(result.message);
                        }else{
                            alert(result.message);
                        }                            
                    }
                    });
                }
                
                return false;
            }
            
            function deletePost(pid){
                if(confirm('Are you sure wan to delete this post?!')){
                    $.ajax({
                    type: 'POST',
                    url: 'action.php',
                    data: {action: 'delPost', pid: pid},
                    success: function(result) {
                        if (!result.error) {
                            location.reload(window.location);
                            alert(result.message);
                        }else{
                            alert(result.message);
                        }                            
                    }
                    });
                }
                
                return false;
            }
            
            
            function unbanUser(uid){
                if(confirm('Are you sure wan to un-ban this member?!')){
                    $.ajax({
                    type: 'POST',
                    url: 'action.php',
                    data: {action: 'unbanUser', uid: uid},
                    success: function(result) {
                        if (!result.error) {
                            location.reload(window.location);
                            alert(result.message);
                        }else{
                            alert(result.message);
                        }                            
                    }
                    });
                    
                }
                
                return false;
            }
            
            function sleep(milliseconds) {
                var start = new Date().getTime();
                for (var i = 0; i < 1e7; i++) {
                    if ((new Date().getTime() - start) > milliseconds){
                        break;
                    }
                }
            }
              
            function addNewPost() {
                var comments2 = document.getElementById('comment').value.length;
                if(comments2===0){
                    $('#comment').addClass('error');
                    $('#commentErr').text("Required Field").show();
                }else{
                    $.ajax({
                    type: 'POST',
                    url: 'action.php',
                    data: $('#addNewPost').serialize(),
                    success: function(result) {
                        if (!result.error) {
                            document.getElementById('comment').value="";
                            $('#commentErr').fadeIn('fast').addClass('success').removeClass('alert').html(result.message).delay(4000).fadeOut('slow');
//                            $(document).ready(function() {
//                            $.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
//                            setInterval(function() {
//                            $('#refreshPost2').load(window.location);
//                            }, 3000); // the "3000" here refers to the time to refresh the div. it is in milliseconds.
//                            });
                            //$('#refreshPost').fadeOut('slow').load('aa.php').fadeIn('slow');
                            //$('#tpost').load(window.location);
                            location.reload(window.location);
                            delay(5000);
                            //document.getElementById("bottom").focus();                                                  
                        } else {
                            $('#commentErr').fadeIn('slow').addClass('alert').removeClass('success').html(result.message).delay(4000).fadeOut('slow');
                        }
                            
                        }
                    });
                }
                }
                
//            $('#comment').keyup(function () {
//                var left = 200 - $(this).val().length;
//                if (left < 0) {
//                    left = 0;
//                }
//                $('#charcounter').text('Characters left: ' + left);
//            });
            
            $(document).ready(function(){
            var left = 500;
            $('#charcounter').text('Characters left: ' + left);

                $('#comment').keyup(function () {

                left = 500 - $(this).val().length;

                if(left < 0){
                    $('#charcounter').addClass("overlimit");
                }
                if(left >= 0){
                    $('#charcounter span').removeClass("overlimit");
                }

                $('#charcounter').text('Characters left: ' + left);
            });
            });
            
            
            function counterChar(pid){
            $(document).ready(function(){
                var left = 500;
                $('#charcounter'+pid).text('Characters left: ' + left);
                $('#commentModify'+pid).keyup(function () {
                    left = 500 - $(this).val().length;
                    $('#charcounter'+pid).text('Characters left: ' + left);
                });
            });
            }
            
        </script>
<!--        
        <div  id="footer_wrap">
            <div id="footer">
                <div id="footer_right">
                    <p align="right">Forum made by Jason Powered by Railway System.</p>
                </div> 
            </div>  End footer
        </div>  End footer_wrap-->

    </body>
</html>
